package cn.dujc.superkid.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.bean.VideoIndexResults;

public class CourseFragmentLeftAdapter extends RecyclerView.Adapter<CourseFragmentLeftAdapter.MyHolder> {
    private static final String TAG = "CourseFragmentLeftAdapt";
    private List<VideoIndexResults.DataBean> data;
    private Context context;
    private OnLeftItemClickListener onLeftItemClickListener;
    private VideoIndexResults.DataBean dataBean;
    private List<VideoIndexResults.DataBean.KnowBean> know;

    public CourseFragmentLeftAdapter(List<VideoIndexResults.DataBean> data, Context context, OnLeftItemClickListener onLeftItemClickListener) {
        this.data = data;
        if(data.size()>0){
            data.get(0).setSelect(true);
        }
        this.context = context;
        this.onLeftItemClickListener = onLeftItemClickListener;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_rv_course_fragment_left, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        if (holder != null) {
            dataBean = data.get(position);
            if(dataBean.isSelect()){
                holder.tvContentCourse.setBackgroundColor(Color.parseColor("#ffffff"));
                holder.tvContentCourse.setTextColor(Color.parseColor("#0096ef"));
            }else{
                holder.tvContentCourse.setBackgroundColor(Color.parseColor("#00000000"));
                holder.tvContentCourse.setTextColor(Color.parseColor("#111111"));
            }
            know = data.get(position).getKnow();
            Log.d(TAG, "onBindViewHolder: know = " + know.toString());
            final String gname = dataBean.getGname();
            Log.d(TAG, "onBindViewHolder: gname=" + gname);
            holder.tvContentCourse.setText(gname);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    ToastUtil.showToast(context, gname);
                    Log.d(TAG, "onClick: position=" + position);
                    Log.d(TAG, "onBindViewHolder: know = " + data.get(position).getKnow().toString());
                    onLeftItemClickListener.onLeftItemClickListener(data.get(position).getKnow());
                    setAllNoSelect();
                    data.get(position).setSelect(true);
                    holder.tvContentCourse.setBackgroundColor(Color.parseColor("#ffffff"));
                    holder.tvContentCourse.setTextColor(Color.parseColor("#0096ef"));
                }
            });
        }
    }

    private void setAllNoSelect() {
        for (VideoIndexResults.DataBean d : data) {
            d.setSelect(false);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {
        TextView tvContentCourse;

        public MyHolder(View itemView) {
            super(itemView);
            tvContentCourse = itemView.findViewById(R.id.tv_content_course);
        }
    }

    public interface OnLeftItemClickListener {
        void onLeftItemClickListener(List<VideoIndexResults.DataBean.KnowBean> know);
    }
}
